package com.fynu.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener {

    int[][] arr = new int[3][3];
    int[][] win = {
            {1,2,3},
            {4,5,6},
            {7,8,0}
    };

    int x = 0;
    int y = 0;

    public GameJFrame(){

        //初始化窗口
        initJFrame();

        //初始化数据
        initDate();

        //初始化标头
        initJMenuBar();
        //初始化图片
        initImage();

        //展示
        this.setVisible(true);
    }

    private void initDate() {

        Random r = new Random();
        int[] temp = {0,1,2,3,4,5,6,7,8};
        for (int i = 0; i < temp.length; i++) {
            int n = r.nextInt(8);
            int t = temp[n];
            temp[n] = temp[i];
            temp[i] = t;
        }
        for (int i = 0; i < temp.length; i++) {

            if(temp[i] == 0){
                x = i/3;
                y = i%3;
            }else if(temp[i] != 0){
                arr[i/3][i%3] = temp[i];
            }
        }

    }

    private void initImage() {

        /*ImageIcon bg = new ImageIcon();
        JLabel background = new JLabel(bg);
        background.setBounds(0,0,320,0);
        this.getContentPane().add(background);*/

        //移除全部图片
        this.getContentPane().removeAll();

        //胜利图片
        /*if(viciory()){
            JLabel winJLable = new JLabel(new ImageIcon());
            winJLable.setBounds(0,0,320,320);
            this.getContentPane().add(winJLable);
        }*/


        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int number = arr[i][j];
                ImageIcon icon = new ImageIcon("1.0_PuzzleGame\\头像\\"+number+".jpg");
                JLabel jLabel = new JLabel(icon);
                jLabel.setBounds(320*j,320*i,320,320);
                //加入边框
                jLabel.setBorder(new BevelBorder(1));
                this.getContentPane().add(jLabel);
                //添加到界面中
                //this.add(jLabel);
            }
        }



        //ImageIcon icon1 = new ImageIcon("1.0_PuzzleGame\\头像\\1.jpg");
        /*ImageIcon icon3 = new ImageIcon("1.0_PuzzleGame\\头像\\"+number+".jpg");
        ImageIcon icon2 = new ImageIcon("1.0_PuzzleGame\\头像\\2.jpg");
        JLabel jLabel1 = new JLabel(new ImageIcon("1.0_PuzzleGame\\头像\\1.jpg"));
        JLabel jLabel2 = new JLabel(icon2);


        jLabel1.setBounds(0,0,320,320);
        jLabel2.setBounds(320,0,320,320);

        this.getContentPane().add(jLabel1);
        this.getContentPane().add(jLabel2);

        this.add(jLabel1);
        this.add(jLabel2);*/
        this.getContentPane().repaint();


    }

    public void initJFrame(){
        this.setSize(960,960);
        //标题
        this.setTitle("拼图游戏1.0");
        //不被打断
        this.setAlwaysOnTop(true);
        //弹窗居中
        this.setLocationRelativeTo(null);
        //关闭形式
        this.setDefaultCloseOperation(3);
        //取消默认居中放置
        this.setLayout(null);

        //鼠标监听
        this.addKeyListener(this);
    }
    public void initJMenuBar(){
        //两个选项
        JMenuBar jMenuBar = new JMenuBar();
        JMenu functionjmenu = new JMenu("功能");
        JMenu aboutjmenu = new JMenu("关于");
        //
        JMenuItem replayItem = new JMenuItem("重新游戏");
        JMenuItem reLoginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");

        JMenuItem accountItem = new JMenuItem("我");
        //
        functionjmenu.add(replayItem);
        functionjmenu.add(reLoginItem);
        functionjmenu.add(closeItem);

        aboutjmenu.add(accountItem);

        jMenuBar.add(functionjmenu);
        jMenuBar.add(aboutjmenu);

        //设置
        setJMenuBar(jMenuBar);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code == 65){
            this.getContentPane().removeAll();
            JLabel allJLable = new JLabel(new ImageIcon("1.0_PuzzleGame\\头像\\all.jpg"));
            allJLable.setBounds(0,0,960,960);
            this.getContentPane().add(allJLable);
            /*ImageIcon bg = new ImageIcon();
            JLabel background = new JLabel(bg);
            background.setBounds(0,0,320,0);
            this.getContentPane().add(background);*/
            this.getContentPane().repaint();

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(viciory()){
            return;
        }

        int code = e.getKeyCode();
        System.out.println(code);

        if(code == 37){
            if(y == 2){
                return;
            }
            //System.out.println("向左");
            arr[x][y] = arr[x][y+1];
            arr[x][y+1] = 0;
            y++;
            initImage();
        }else if(code == 38){
            //System.out.println(x + " "+y);
            //System.out.println("向上");
            if(x==2){
                return;
            }
            arr[x][y] = arr[x+1][y];
            arr[x+1][y] = 0;
            x++;
            //System.out.println(arr.length);
            initImage();
            //System.out.println(x + " " + y);
        }else if(code == 39){
            if(y == 0){
                return;
            }
            //System.out.println("向右");
            arr[x][y] = arr[x][y-1];
            arr[x][y-1] = 0;
            y--;
            initImage();
        }else if(code == 40){
            if(x == 0){
                return;
            }
            //System.out.println("向下");
            arr[x][y] = arr[x-1][y];
            arr[x-1][y] = 0;
            x--;
            initImage();
        }else if(code == 65){
            initImage();
        }/*else if(code == 87){
            arr = new int[][]{
                    {1,2,3},
                    {4,5,6},
                    {7,8,0},
            };
            initImage();
        }*/
    }
    public boolean viciory(){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if(arr[i][j] != win[i][j]){
                    return false;
                }
            }
        }
        return true;
    }

}






